<?php

function get_where($param){
    $where = array();
	
    $where_str = "";
    if(isset($param['role_id']) && $param['role_id']){
        $where[] = "$5=={$param['role_id']}";
    }
    if(!empty($where)){
        $where_str = " && ".implode(" && ",$where);
    }
    return $where_str;
}

/**
 *  
 * @param $param
 */
//http://log.bajian.wan.sogou.com/index.php?a=get_binding_ingot_expend_log&server_id=1&begin_time=1990-01-01
function get_binding_ingot_expend_log($param){

    $where_str = get_where($param);
    $cmd = <<<EOT
cat {$param['log_path']}/binding_ingot_expend_[0-9]*.txt |awk -F" " '{print $3}'|awk -F"," '{if($7>=%s && $7<=%s %s){print $0}else{}}'
EOT;
    $cmd = sprintf($cmd,$param['begin_time'],$param['end_time'],$where_str);
 
    $data = exec_cmd($cmd,$param);
//    var_dump($res,$data);
/*
购买道具ID
购买道具数量
购买道具单价
本次购买总花费绑定元宝
购买后绑定元宝余额
购买时的角色等级
购买时角色所在地图位置
item_id
item_num
unit_price
binding_ingot_cost
binding_ingot_blance
role_level
map_id
*/
    $log = array();
    foreach($data['data'] as $k=>$v){
        $log_arr = explode(",",$v);
        $log[] = array(
			'gid'=>$log_arr[0],
			'sid'=>$log_arr[1],
			'oldsid'=>$log_arr[2],
			'user_id'=>$log_arr[3],
			'role_id'=>$log_arr[4],
			'part_id'=>$log_arr[5],
			'time'=>floor($log_arr[6]/1000),
			'item_id'=>$log_arr[7],
			'item_num'=>$log_arr[8],
			'unit_price'=>$log_arr[9],
			'binding_ingot_cost'=>$log_arr[10],
			'binding_ingot_blance'=>$log_arr[11],
			'role_level'=>$log_arr[12],
			'map_id'=>$log_arr[13],
		);
    }
    ajax_return($data['total'],$data['per_page'],$data['total_page'],$data['page'],$log);
}